package com.uenpay.oem.data.modelImpl;

import com.uenpay.oem.common.TransCodeConstant;
import com.uenpay.oem.data.bean.Request;
import com.uenpay.oem.data.model.ITransactionModel;
import com.uenpay.oem.net.RetrofitHttpService;
import com.uenpay.util.net.http.BaseRequest;
import com.uenpay.util.net.http.IHttpManager;
import com.uenpay.util.net.listener.HttpOnNextListener;
import com.uenpay.util.net.listener.HttpResultListener;
import com.uenpay.util.net.retrofit.HttpManager;
import com.uenpay.util.net.retrofit.RetrofitRequest;

import java.util.HashMap;
import java.util.Map;

import rx.Observable;

/**
 * Author: liulou Date: 2017/6/13. desc:
 */

public class TransactionModel implements ITransactionModel {

    private IHttpManager manager = null;

    public TransactionModel() {
        manager = HttpManager.getInstance(RetrofitHttpService.class);
    }

    @Override
    public void checkCode(final String tradeNo, final String wkPayNo, final String yzm, HttpResultListener listener) {
        if (manager == null) {
            return;
        }
        BaseRequest checkCodeReq = new RetrofitRequest(listener) {
            @Override
            public Observable getObservable(Object methods) {
                Request request = new Request(TransCodeConstant.CODE_TRANSCATION_CHECK_CODE);
                Map<String, String> body = new HashMap<>();
                body.put("tradeNo", tradeNo);
                body.put("wkPayNo", wkPayNo);
                body.put("yzm", yzm);
                request.setBody(body);
                return ((RetrofitHttpService) methods).checkCode(request);
            }
        };
        manager.doHttpDeal(checkCodeReq);
    }

    @Override
    public void sendQuery(String tradeNo, HttpResultListener listener) {

    }

    /**
     * 费率的接口
     *
     * @param tradeType 交易类型
     */
    @Override
    public void counterFee(final String tradeType, HttpResultListener listener) {
        if (manager == null) {
            return;
        }
        BaseRequest tradePoundage = new RetrofitRequest(listener) {
            @Override
            public Observable getObservable(Object methods) {
                Request request = new Request(TransCodeConstant.CODE_GET_TRADE_POUNDAGE);
                Map<String, String> body = new HashMap<>();
                body.put("tradeType", tradeType);

                request.setBody(body);
                return ((RetrofitHttpService) methods).getUserTradePoundage(request);
            }
        };
        manager.doHttpDeal(tradePoundage);
    }

    /**
     * @param amount         提现金额
     * @param userBankCardId 银行卡主键id
     * @param listener       counterFee  费率
     */
    @Override
    public void withdraw(final String amount, final String userBankCardId, final String counterFee, final String payToken, HttpResultListener listener) {
        if (manager == null) {
            return;
        }
        BaseRequest withDrawReq = new RetrofitRequest(listener) {
            @Override
            public Observable getObservable(Object methods) {
                Request request = new Request(TransCodeConstant.CODE_TRANSCATION_WITH_DRAW);
                Map<String, String> body = new HashMap<>();
                body.put("amount", amount);
                body.put("userBankCardId", userBankCardId);
                body.put("counterFee", counterFee);
                body.put("payToken", payToken);
                request.setBody(body);
                return ((RetrofitHttpService) methods).withDraw(request);
            }
        };
        manager.doHttpDeal(withDrawReq);
    }

    @Override
    public void transfer(final String amount, final String oprOpenIds, final String payPassword, final String payType, final String memo, HttpOnNextListener<HashMap<String, String>> listener) {
        if (manager == null) {
            return;
        }
        BaseRequest tranferReq = new RetrofitRequest(listener) {
            @Override
            public Observable getObservable(Object methods) {
                Request request = new Request(TransCodeConstant.CODE_GET_TRADE_TRANSFER);
                Map<String, String> body = new HashMap<>();
                body.put("amount", amount);
                body.put("oprOpenIds", oprOpenIds);
                body.put("payPassword", payPassword);
                body.put("payType", payType);
                body.put("memo", memo);
                request.setBody(body);
                return ((RetrofitHttpService) methods).transfer(request);
            }
        };
        manager.doHttpDeal(tranferReq);
    }
}
